package com.kangye.exam.service.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Itembank;
import com.kangye.exam.bean.Paper;
import com.kangye.exam.dao.PaperDao;
import com.kangye.exam.service.PaperService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "paperService")
public class PaperServiceImpl implements PaperService {

    @Resource
    private PaperDao paperDao;

    //通过插件分页就不选想数据库limit语句通过PageInfo进行自动分页
    @Override
    public PageInfo<Itembank> selectBysubjectName(String subjectName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("subjectName", subjectName);
        List<Itembank> list = paperDao.selectbysubject(map);
        PageInfo<Itembank> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo selectByid(Integer itemId, String typeName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("id", itemId);
        map.put("typeName", typeName);
        List<Itembank> list = paperDao.selectbyId(map);
        PageInfo<Itembank> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Integer createpaper(String paperName, String paperPath, Integer paperTime, Integer teahcerId, String subjectName) {
        Map<String, Object> map = new HashMap<>();
        map.put("teacherId", teahcerId);
        map.put("paperName", paperName);
        map.put("paperPath", paperPath);
        map.put("paperTime", paperTime);
        map.put("subjectName", subjectName);
        return paperDao.createpaper(map);
    }

    @Override
    public PageInfo paperall(Integer teacherId, Integer paperId, Integer subjectId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("teacherId", teacherId);
        map.put("paperId", paperId);
        map.put("subjectId", subjectId);
        List<Paper> list = paperDao.paperall(map);
        PageInfo<Paper> paperPageInfo = new PageInfo<>(list);
        return paperPageInfo;
    }

    @Override
    public Integer delpaper(Integer paperId) {
        Map<String, Object> map = new HashMap<>();
        map.put("paperId", paperId);
        if (paperDao.delpaper(map) == 1) {
            return 0;
        } else {
            return 1;
        }

    }


}
